package com.jerry.huihome.module.find.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.module.find.entity.Dynamic;
import com.jerry.huihome.utils.TextParser;
import com.jerry.huihome.views.ExpandGridView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/5/11.
 */
public class DynamicAdapter extends BaseAdapter {

    private Context context;

    private List<Dynamic.BodyBean.DynaItemsBean> list;
    private GirdViewListImageAdapter adapter;
    private DisplayImageOptions options;
    private PopupWindow mPopupWindow;

    public DynamicAdapter(Context context, List<Dynamic.BodyBean.DynaItemsBean> list) {
        this.list = list;
        this.context = context;
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }

    public List<Dynamic.BodyBean.DynaItemsBean> getList(){
        return list;
    }
    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Dynamic.BodyBean.DynaItemsBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder = null;
        Dynamic.BodyBean.DynaItemsBean item = getItem(position);
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.dynamic_list_item_1img, null);
                    mViewHolder = new MyViewHolder1();
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.dynamic_list_item, null);
                    mViewHolder = new MyViewHolder2();
                    break;
            }
            ButterKnife.bind(mViewHolder, convertView);

            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        switch (type) {
            case 1:
                MyViewHolder1 holder1 = (MyViewHolder1) mViewHolder;
                if (item.getProjname() != null) {
                    holder1.llSynchronize1.setVisibility(View.VISIBLE);
                    holder1.tvSynchronize1.setText("《" + item.getProjname() + "》");
                }else {
                    holder1.llSynchronize1.setVisibility(View.GONE);
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String time = format.format(new Date(item.getOccurTime()));
                holder1.tvCommentTime1.setText(time);
                holder1.tvDetailComment1.setText(item.getMsgtxt());
                BaseApplication.getApplication().getImageLoader().displayImage(item.getDynaImages().get(0).getListImg(), holder1.IvImg, options);
                if (item.getAppraises() == null) {
                    holder1.llHeart1.setVisibility(View.GONE);
                } else {
                    holder1.tvHeartName1.setText("");
                    holder1.llHeart1.setVisibility(View.VISIBLE);
                    List<Dynamic.BodyBean.DynaItemsBean.AppraisesBean> appraises = item.getAppraises();
                    List<String> listNames = new ArrayList<>();
                    for (int i = 0; i < appraises.size(); i++) {
                        listNames.add(appraises.get(i).getUsername());
                    }
                    StringBuffer name = new StringBuffer();
                    for (int i = 0; i < listNames.size(); i++) {
                        if (i == listNames.size() - 1) {
                            name.append(listNames.get(i));
                        } else {
                            name.append(listNames.get(i) + ";");
                        }
                    }
                    holder1.tvHeartName1.setText(name);
                }
                if (item.getComments() == null) {
                    holder1.llChat1.setVisibility(View.GONE);
                } else {
                    holder1.llChat1.removeAllViews();
                    holder1.llChat1.setVisibility(View.VISIBLE);
                    List<Dynamic.BodyBean.DynaItemsBean.CommentsBean> comments = item.getComments();
                    for (int i = 0; i < comments.size(); i++) {
                        TextParser textParser = new TextParser();
                        TextView textView = new TextView(context);
                        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                        if (comments.get(i).getToUsername() == null) {
                            textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                            textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                        } else {
                            textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                            textParser.append("回复", 56, Color.BLACK);
                            textParser.append((String) comments.get(i).getToUsername(), 56, Color.BLUE);
                            textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                        }
                        textParser.parse(textView);
                        holder1.llChat1.addView(textView);
                    }
                }
                holder1.ivMessageImg1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mPopupWindow != null){
                            initPopupWindow(v);
                        }
                    }
                });
                break;
            case 2:
                MyViewHolder2 holder2 = (MyViewHolder2) mViewHolder;
                if (item.getProjname() != null) {
                    holder2.llSynchronize.setVisibility(View.VISIBLE);
                    holder2.tvSynchronize2.setText("《" + item.getProjname() + "》");
                } else {
                    holder2.llSynchronize.setVisibility(View.GONE);
                }
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String time2 = format2.format(new Date(item.getOccurTime()));
                holder2.tvCommentTime2.setText(time2);
                holder2.tvDetailComment2.setText(item.getMsgtxt());
                if (item.getAppraises() == null) {
                    holder2.ivHeart.setVisibility(View.GONE);
                    holder2.llHeart2.setVisibility(View.GONE);
                } else {
                    holder2.llHeart2.setVisibility(View.VISIBLE);
                    holder2.ivHeart.setVisibility(View.VISIBLE);
                    holder2.tvHeartName2.setText("");
                    List<Dynamic.BodyBean.DynaItemsBean.AppraisesBean> appraises = item.getAppraises();
                    List<String> listNames = new ArrayList<>();
                    for (int i = 0; i < appraises.size(); i++) {
                        listNames.add(appraises.get(i).getUsername());
                    }
                    StringBuffer name = new StringBuffer();
                    for (int i = 0; i < listNames.size(); i++) {
                        if (i == listNames.size() - 1) {
                            name.append(listNames.get(i));
                        } else {
                            name.append(listNames.get(i) + ";");
                        }
                    }
                    holder2.tvHeartName2.setText(name);
                }
                List<Dynamic.BodyBean.DynaItemsBean.CommentsBean> comments = item.getComments();
                if (comments == null) {
                    holder2.llChat2.setVisibility(View.GONE);
                } else {
                    holder2.llChat2.removeAllViews();
                    holder2.llChat2.setVisibility(View.VISIBLE);
                    for (int i = 0; i < comments.size(); i++) {
                        TextView textView = new TextView(context);
                        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                        TextParser textParser = new TextParser();
                        if (comments.get(i).getToUsername() == null) {
                            textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                            textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                        } else {
                            textParser.append(comments.get(i).getFromUsername(), 56, Color.BLUE);
                            textParser.append("回复", 56, Color.BLACK);
                            textParser.append((String) comments.get(i).getToUsername(), 56, Color.BLUE);
                            textParser.append(comments.get(i).getMsgtxt(), 56, Color.BLACK);
                        }
                        textParser.parse(textView);
                        holder2.llChat2.addView(textView);
                    }
                }
                holder2.ivMessageImg2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initPopupWindow(v);
                    }
                });
                List<Dynamic.BodyBean.DynaItemsBean.DynaImagesBean> dynaImages = item.getDynaImages();

                List<String> strings = new ArrayList<>();
                for (int i = 0; i < dynaImages.size(); i++) {
                    strings.add(dynaImages.get(i).getListImg());
                }
                adapter = new GirdViewListImageAdapter(context, strings);
                holder2.egvImg.setAdapter(adapter);
                break;

        }
        return convertView;
    }

    private void initPopupWindow(View v) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow, null);
        mPopupWindow = new PopupWindow(view, 600,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        // 点击空白区域和默认返回按钮取消弹出Window
        // 一般设置背景取消弹出Window
        ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        //显示在左边
        mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
                location[0]-mPopupWindow.getWidth(), location[1]-15);
    }




    @Override
    public int getItemViewType(int position) {
        Dynamic.BodyBean.DynaItemsBean dynaItemsBean = getItem(position);
        List<Dynamic.BodyBean.DynaItemsBean.DynaImagesBean> dynaImages = dynaItemsBean.getDynaImages();
        if (dynaImages.size() > 1) {
            return 2;
        } else {
            return 1;
        }

    }

    class ViewHolder {

    }

    class MyViewHolder1 extends ViewHolder {
        @BindView(R.id.tv_synchronize1)
        TextView tvSynchronize1;
        @BindView(R.id.ll_synchronize1)
        LinearLayout llSynchronize1;
        @BindView(R.id.tv_detail_comment1)
        TextView tvDetailComment1;
        @BindView(R.id.iv_img)
        ImageView IvImg;
        @BindView(R.id.tv_synchronize_place1)
        TextView tvSynchronizePlace1;
        @BindView(R.id.tv_reform_type1)
        TextView tvReformType1;
        @BindView(R.id.tv_comment_time1)
        TextView tvCommentTime1;
        @BindView(R.id.iv_message_img1)
        ImageView ivMessageImg1;
        @BindView(R.id.tv_heart_name1)
        TextView tvHeartName1;
        @BindView(R.id.ll_chat1)
        LinearLayout llChat1;
        @BindView(R.id.ll_heart1)
        LinearLayout llHeart1;
    }

    class MyViewHolder2 extends ViewHolder {

        @BindView(R.id.tv_synchronize2)
        TextView tvSynchronize2;
        @BindView(R.id.tv_detail_comment2)
        TextView tvDetailComment2;
        @BindView(R.id.egv_img)
        ExpandGridView egvImg;
        @BindView(R.id.tv_comment_time2)
        TextView tvCommentTime2;
        @BindView(R.id.iv_message_img2)
        ImageView ivMessageImg2;
        @BindView(R.id.tv_heart_name2)
        TextView tvHeartName2;
        @BindView(R.id.ll_chat2)
        LinearLayout llChat2;
        @BindView(R.id.ll_synchronize)
        LinearLayout llSynchronize;
        @BindView(R.id.ll_heart2)
        LinearLayout llHeart2;
        @BindView(R.id.iv_heart)
        ImageView ivHeart;
    }
}
